package com.shiguiwu.springmybatis.javabase.nio.socket;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.charset.StandardCharsets;

/**
 * @description: 接受方
 * @author: stone
 * @date: Created by 2021/11/8 16:04
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.nio.socket
 */
public class DatagramReceive {

    public static void main(String[] args) throws Exception {
        //创建一个接受方socket
        DatagramChannel receive = DatagramChannel.open();
        InetSocketAddress inetSocketAddress = new InetSocketAddress(9999);

        //绑定
        receive.bind(inetSocketAddress);

        //创建一个接受缓存去
        ByteBuffer allocate = ByteBuffer.allocate(1024);

        while (true) {

            allocate.clear();

            SocketAddress socketAddress = receive.receive(allocate);

            allocate.flip();

            System.out.println(socketAddress.toString());

            System.out.println(StandardCharsets.UTF_8.decode(allocate));


        }



    }
}
